<template>
  <div>
    <span>全选:</span>
    <!-- v-model关联全选框选中状态thisAll -->
    <input type="checkbox" v-model= 'thisAll'/>
    <button @click="btn">反选</button>
    <ul>
      <li v-for="(obj,index) in arr " :key='index'>
        <!-- 单选框关联对象中的属性，实现双向绑定 -->
        <input type="checkbox" v-model='obj.c'/>
        <span>{{obj.name}}</span>
      </li>
    </ul>
  </div>
</template>
<!-- 目标
1.渲染静态页面
2.填充数据
3.将对象的c属性，用vue-model双向绑定
-->
<script>
export default {
  data() {
    return {
      arr: [
        {
          name: "猪八戒",
          c: false,
        },
        {
          name: "孙悟空",
          c: false,
        },
        {
          name: "唐僧",
          c: false,
        },
        {
          name: "白龙马",
          c: false,
        },
      ],
    };
  },
  computed:{
    thisAll:{
      set(value){
         return this.arr.forEach(obj => obj.c = value)
      },
      get(){
        return this.arr.every(obj =>obj.c === true)
      }
    }
  },
  methods:{
    btn(){
      // 遍历数组获取每一项c属性,将c属性值取反然后重新赋值回去
      this.arr.forEach(obj => obj.c = !obj.c)
    }
  }

}
</script>